//
//  HDPayoutDetailTableViewCell.swift
//  HuaDaoPeifu
//
//  Created by 刘汉琳 on 16/4/5.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit
import SDWebImage

protocol HDPayOutDetailCellDelegate:class {
    func addImageClickDelegate(_ sender:HDImageBtn)
    func showBigImageClickDelegate(_ sender:HDImageBtn)

}

class HDPayoutDetailTableViewCell: UITableViewCell {
    
    weak var delegate:HDPayOutDetailCellDelegate?
    var isShow:String!
    var picImageView = UIImageView()

    var nameArray = [String]()
    var case_ID = String()
    var imageArray = [HDUploadImageModel]()
    var status:String!
    var casefailModelArray:[HDPayoutCasefailModel]!


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: UITableViewCellStyle.default, reuseIdentifier: reuseIdentifier)
        
        
    }
    var payDetailModel:HDPayoutDetailModel? {
        didSet {
            isShow = payDetailModel?.isShow
            if let detailModel = payDetailModel  {
                self.nameArray = detailModel.nameArray
                case_ID = detailModel.caseid
                self.imageArray = detailModel.uploadImageArray
                self.status = detailModel.status
                casefailModelArray = detailModel.casefailModelArray
                setUpCell((payDetailModel?.indexPath)!,sectionImageArray: detailModel.imageArray)

            }
        }
    }
    
    
    
    init(dataStr:String, reuseIdentifier cellId:String,index:IndexPath,detailModel:HDPayoutDetailModel) {
        super.init(style: UITableViewCellStyle.default, reuseIdentifier: cellId)
        isShow = dataStr
        self.nameArray = detailModel.nameArray
        case_ID = detailModel.caseid
        self.imageArray = detailModel.uploadImageArray
        self.status = detailModel.status
        casefailModelArray = detailModel.casefailModelArray
        setUpCell(index,sectionImageArray: detailModel.imageArray)
    }
    
    func setUpCell(_ indexPath:IndexPath,sectionImageArray:[HDPayoutCaseInitModel]){
        var model = HDPayoutCaseInitModel()
        
            for subView in self.contentView.subviews {
                subView.removeFromSuperview()
            }

            if isShow != "notShow" {
                if (sectionImageArray.count > 0) {
                    let number = sectionImageArray.count
                    for index in 0..<number{
                        model = sectionImageArray[index]
                        createBtnImageView(index,add: "notAdd",section: indexPath.section,model: model)
                    }
                    if self.status != "-1" && self.status != "0" {
                        if sectionImageArray.count - payDetailModel!.haveUploadImageInter < 10 {
                            if self.status == "1" {
                            }else{
                                
                                for index in 0..<self.casefailModelArray.count {
                                    let str = self.casefailModelArray[index].datatype
                                    if str == nameArray[indexPath.section] {
                                        createBtnImageView(number,add: "add",section: indexPath.section,model: model)
                                    }
                                }
                            }
                        }
                        
                    }else{
                        if(number<10){
                            createBtnImageView(number,add: "add",section: indexPath.section,model: model)
                        }
                    }
                    
                }else{
                    if self.status != "1" && self.status != "3" && self.status != "4" && self.status != "6" {
                        createBtnImageView(0,add: "add",section: indexPath.section,model: model)
                    }else{
                        createLine()
                    }
                    
                    
                }
            }
        
    }
    
    func createBtnImageView(_ number: Int, add:String,section:NSInteger,model:HDPayoutCaseInitModel)->HDImageBtn{
        
        let index_X = number%4
        let index_Y = number/4
        let imageHeightOrWith = Int(AppConstant.screenWidth - 20*5 - 14)/4
        let imageBtn = HDImageBtn()
        imageBtn.selectIndexPath = section
        imageBtn.selectImageNumber = number
        imageBtn.layer.cornerRadius = 2.5
        imageBtn.layer.masksToBounds = true
        imageBtn.btnImageView = UIImageView()
        imageBtn.btnImageView?.contentMode = .scaleAspectFill


        self.contentView.addSubview(imageBtn)
        var imageBtn_left:Int = 0
        if self.status != "-1" && self.status != "0" {
            imageBtn_left += 10
            createLine()
        }
        imageBtn.snp.removeConstraints()
        imageBtn.snp.makeConstraints { (make) in
            make.top.equalTo(15 + (imageHeightOrWith + 15) * index_Y)
            make.left.equalTo(imageBtn_left + 27 + (imageHeightOrWith + 20) * index_X)
            make.height.equalTo(imageHeightOrWith)
            make.width.equalTo(imageHeightOrWith)
        }

        if(add == "add"){
            imageBtn.btnImageView!.image = UIImage(named: "添加上传")
            
            imageBtn.addTarget(self, action: #selector(HDPayoutDetailTableViewCell.addImageClick(_:)), for: .touchUpInside)
        }else{
            if payDetailModel?.uploadError == "error"{
                
                if number >= payDetailModel!.haveUploadImageInter{
                        let iconFuzzy = UIImageView()
                        imageBtn.addSubview(iconFuzzy)
                        iconFuzzy.snp.makeConstraints({ (make) in
                            make.bottom.equalTo(imageBtn).offset(0)
                            make.right.equalTo(imageBtn).offset(0)
                            make.width.equalTo(13)
                            make.height.equalTo(13)
                        })
                        iconFuzzy.image = UIImage(named: "上传失败_icon")
                }
            }else if model.imageStatus == .fuzzy && (self.status == "2" || self.status == "5" || self.status == "-1" || self.status == "0") {
                let iconFuzzy = UIImageView()
                imageBtn.addSubview(iconFuzzy)
                iconFuzzy.snp.makeConstraints({ (make) in
                    make.bottom.equalTo(imageBtn).offset(0)
                    make.right.equalTo(imageBtn).offset(0)
                    make.width.equalTo(13)
                    make.height.equalTo(13) 
                })
                iconFuzzy.image = UIImage(named: "模糊提示")
                
            }
            if self.imageArray[number].image != nil {
                imageBtn.btnImageView?.image = self.imageArray[number].image // (self.imageArray[number].image, forState: .Normal)

            }
            else{
    
                
                imageBtn.btnImageView?.sd_setImage(with: URL(string: model.img_small)!, placeholderImage: UIImage(named: "图片默认图"),options: .retryFailed, completed:{ (image, error, cacheType, url) in
                 if image != nil{
                        
                        HDSaveImage.saveImageWithImageName(image!, caseId: self.case_ID, dataType: self.nameArray[section], imageCount: number)
                    }
                })
                
               
                
                
            }

            
            imageBtn.addTarget(self, action: #selector(HDPayoutDetailTableViewCell.showBigImageClick(_:)), for: .touchUpInside)
        }
        
        return imageBtn
    }
    
    func addImageClick(_ sender:HDImageBtn){
        if (delegate != nil) {
            self.delegate?.addImageClickDelegate(sender)
        }
        
    }
    
    func showBigImageClick(_ sender:HDImageBtn){
        if (delegate != nil) {
            self.delegate?.showBigImageClickDelegate(sender)
        }
        
    }
    
    func createLine(){
        let lineLabel = UILabel()
        contentView.addSubview(lineLabel)
        lineLabel.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.left.equalTo(18)
            make.width.equalTo(1)
            make.bottom.equalTo(0)
        }
        lineLabel.backgroundColor = UIColor(hex:0xd5d5d5)
    }
}
